//
//  ByUINRegisterVC.m
//  oc_knowledge
//
//  Created by justiOS on 17/1/16.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByUINRegisterVC.h"

// vc
#import "ByUINFillCodeVC.h"                     // 验证验证码
// net
#import "ByApiUser.h"
// view
#import "ByViewRegisterStep.h"
#import "UILabel+YBAttributeTextTapAction.h"
@interface ByUINRegisterVC ()

@property (nonatomic, strong) UITextField *mobleTF;
@property (nonatomic, strong) UIButton *codeButton;
@property (nonatomic, strong) UIButton *deleButton;
@property (nonatomic, assign) BOOL isRead;
@end

@implementation ByUINRegisterVC

#pragma mark getter
- (UITextField *)mobleTF {
    if (!_mobleTF) {
        _mobleTF = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, SCREEN_WIDTH - 40, 20)];
        _mobleTF.placeholder = @"请输入您的手机号码";
        _mobleTF.tintColor = BY_THEME_COLOR;
        _mobleTF.keyboardType = UIKeyboardTypeNumberPad;
        _mobleTF.font = FONT(14);
        [_mobleTF addTarget:self action:@selector(mobleTFChange:) forControlEvents:UIControlEventEditingChanged];
        [_mobleTF becomeFirstResponder];
    }
    return _mobleTF;
}

- (UIButton *)codeButton {
    if (!_codeButton) {
        _codeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _codeButton.layer.cornerRadius = 3;
        _codeButton.backgroundColor = rgba(124, 203, 242, 1);
        [_codeButton setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_codeButton addTarget:self action:@selector(codeAction:) forControlEvents:UIControlEventTouchUpInside];
        _codeButton.enabled = NO;
    }
    return _codeButton;
}

- (UIButton *)deleButton {
    if (!_deleButton) {
        _deleButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleButton setImage:[UIImage imageNamed:@"Register_delete"] forState:UIControlStateNormal];
        [_deleButton addTarget:self action:@selector(deleAction:) forControlEvents:UIControlEventTouchUpInside];
        _deleButton.hidden = YES;
    }
    return _deleButton;
}


#pragma mark view
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    [self addLeftBackBarButtonItem];
    [self createView];
    self.view.backgroundColor = TABLE_COLOR;
    
    self.view.userInteractionEnabled = true;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapEmptyAction:)];
    [self.view addGestureRecognizer:tap];

}


- (void)createView {
    ByViewRegisterStep *view = [[ByViewRegisterStep alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    view.step = RegisterStepOne;
    [self.view addSubview:view];
    
    UIView *TFBG = [[UIView alloc] initWithFrame:CGRectMake(0, 50, SCREEN_WIDTH, 40)];
    TFBG.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:TFBG];
    [TFBG addSubview:self.mobleTF];
    self.deleButton.frame = CGRectMake(SCREEN_WIDTH - 30, 10, 20, 20);
    [TFBG addSubview:self.deleButton];
    
    
    self.codeButton.frame = CGRectMake(10, TFBG.bottom + 10, SCREEN_WIDTH - 20, 40);
    [self.view addSubview:self.codeButton];
    
    UIButton *readButton = [UIButton buttonWithType:UIButtonTypeCustom];
    readButton.frame = CGRectMake(10, self.codeButton.bottom + 12.5, 15, 15);
    [readButton setImage:[UIImage imageNamed:@"Register_selected"] forState:UIControlStateNormal];
    [readButton addTarget:self action:@selector(readAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:readButton];
    
    
    UILabel *delegateLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, self.codeButton.bottom + 10, SCREEN_WIDTH - 50, 20)];
    NSString *str1 = @"我已阅读并同意《用户服务协议》";
    
    delegateLabel.textColor = [UIColor lightGrayColor];
    delegateLabel.adjustsFontSizeToFitWidth = YES;
    delegateLabel.numberOfLines = 0;
    delegateLabel.enabledTapEffect = NO;
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:str1];
    [attrStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:14] range:NSMakeRange(0, str1.length)];
    [attrStr addAttribute:NSForegroundColorAttributeName value:BY_THEME_COLOR range:NSMakeRange(7, 8)];
    [delegateLabel setAttributedText:attrStr];
    [self.view addSubview:delegateLabel];
    [delegateLabel yb_addAttributeTapActionWithStrings:@[@"《用户服务协议》"] tapClicked:^(NSString *string, NSRange range, NSInteger index) {
        NSString *str = [NSString stringWithFormat:@"https://api.ryzcgf.com/public/web.php/web/copyright"];
        ByWebViewController *vc = [[ByWebViewController alloc] initWithTotalUrlString:str];
        vc.title = @"用户服务协议";
        [self by_normalPush:vc hasNavi:YES];
    }];

}


#pragma mark 点击事件
- (void)codeAction:(UIButton *)button {
    if (_isRead) {
        [ByCoreToolAlert showOnlyText:@"请阅读用户协议"];
        return;
    }
    
    if (_mobleTF.text.length != 11 ) {
        [ByCoreToolAlert showOnlyText:@"请填写正确电话号码"];
        return;
    }
    
    ByNetParamsModel *model = [[ByNetParamsModel alloc] init];
    model.mobile = self.mobleTF.text;
    model.type = @"1";
    [ByCoreToolAlert showLoadind:@"发送中..." hasMasker:YES];
    [ByApiUser getCode:model success:^(ByNetBaseResult *result) {
        [ByCoreToolAlert hideHud];
        ByUINFillCodeVC *vc = [[ByUINFillCodeVC alloc] init];
        vc.phoneNumber = _mobleTF.text;
        vc.passcode = result.data;
        [self by_normalPush:vc hasNavi:YES];
    } failure:^(ByNetBaseResult *result) {
        [ByCoreToolAlert showFailureWithText:result.msg];
    }];

}


- (void)readAction:(UIButton *)button {
    self.isRead = !self.isRead;
    if (_isRead) {
        [button setImage:[UIImage imageNamed:@"Register_unselected"] forState:UIControlStateNormal];
    }else{
        [button setImage:[UIImage imageNamed:@"Register_selected"] forState:UIControlStateNormal];
    }
}

- (void)deleAction:(UIButton *)button {
    self.mobleTF.text = nil;
    button.hidden = YES;
}

- (void)tapEmptyAction:(UITapGestureRecognizer *)tap {
    
    [UIView animateWithDuration:0.4 animations:^{
        [self.mobleTF resignFirstResponder];
    }];
}
#pragma mark tf change
- (void)mobleTFChange:(UITextField *)textField {
    self.codeButton.enabled = NO;
    self.deleButton.hidden = YES;
    if (textField.text.length > 0) {
        self.deleButton.hidden = NO;
    }
    
    if (textField.text.length == 11) {
        self.codeButton.enabled = YES;
    }
    if (textField.text.length > 11) {
        textField.text = [textField.text substringToIndex:11];
    }
    self.codeButton.enabled ? (self.codeButton.backgroundColor = BY_THEME_COLOR) : (self.codeButton.backgroundColor = rgba(124, 203, 242, 1));
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
